package com.trkj.usercenter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.base.User;
import com.trkj.base.utils.UserService;
import com.trkj.taoruiyun.R;

import java.text.MessageFormat;

/**
 * Created by LiZanhong on 2015/06/25.
 */
public class ScoreFragment extends BaseDataRefreshFragment{

    public int getUid() {
        User user = UserService.getInstance(this.getActivity()).parseUser();
        return user != null ? user.userId : 0;
    }

    @Override
    public String getUrl() {
        String url = MessageFormat.format(Constants.ApiUrl.SCORE, getUid());
        Log.d("URL", url);
        return url;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new ScoreAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    class ScoreAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            if(convertView == null){
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(
                        R.layout.activity_score_item, null);
                viewHolder.scoreReason = (TextView)convertView.findViewById(
                        R.id.score_reason);
                viewHolder.howPointers = (TextView)convertView.findViewById(
                        R.id.how_pointers);
                viewHolder.scoreDate = (TextView)convertView.findViewById(
                        R.id.score_date);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }

            JSONArray data = getData();
            viewHolder.scoreReason.setText(data.getJSONObject(position).getString("desc_"));

            int score =  data.getJSONObject(position)
                    .getIntValue("how_points");
            if(score >= 0) {
                viewHolder.howPointers.setText("获得学分" + score + "分");
            }else{
                viewHolder.howPointers.setText("扣除学分" + Math.abs(score) + "分");
            }
            viewHolder.scoreDate.setText(
                    data.getJSONObject(position)
                            .getString("points_time")
                            .substring(0, 10));

            return convertView;
        }

        class ViewHolder{
            TextView scoreReason;
            TextView howPointers;
            TextView scoreDate;
        }
    }

}
